<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/list/models/List.php';
require_once 'modules/product/models/Input.php';
require_once 'modules/product/models/Product.php';

class default_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		$this->view->headTitle('Home');
		$this->view->menuId = 'home';
		
		$objCProduct = new Models_ProductCategory();
		
		/**
		 * Get all product category
		 */
		$allCats = $objCProduct->getAllProductCatWithParent(null,2);
		foreach($allCats as &$cat) {
			if (!empty($cat['child'])) {
				foreach ($cat['child'] as &$child) {
					$child['url'] = Nine_Route::_(Nine_Registry::getBaseUrl()."product/index/index/cid/{$child['product_category_gid']}");
				}
				unset($child);
			}
		}
		unset($cat);
		
		$this->view->allCats = $allCats;

		/**
		 * Get latest news
		 */
		$objContent = new Models_Content();
		
		$latestNews = $objContent->getLatestContentByCategory(2);
		
		if (null != @$latestNews['intro_text']) {
			$latestNews['intro_text'] = Nine_Function::subStringAtBlank($latestNews['intro_text'],350);
		}
		
		if (null != @$latestNews['images']) {
        	$tmp = explode('||', trim($latestNews['images'],"|| "));
			$latestNews['main_image'] = Nine_Function::getThumbImage(@$tmp[0],287);
        }
        else {
        	$latestNews['main_image'] = Nine_Function::getThumbImage('media/userfiles/images/noImage.jpg',287);
        }
		
        $latestNews['url'] = Nine_Route::_(Nine_Registry::getBaseUrl()."content/index/detail/id/{$latestNews['content_gid']}");
        
        $this->view->latestNews = $latestNews;
        
        $this->view->staticContent = $objContent->getContentByGid(24)->toArray();
        
        
        /**
         * Get featured
         */
        $objProduct = new Models_Product();
        $featuredProduct = $objProduct->getRandomFeaturedProduct();
		
//        $featuredProduct['title'] = @Nine_Function::subStringAtBlank($featuredProduct['title'],15);
        
        if (null != @$featuredProduct['intro_text']) {
        	$featuredProduct['summary'] = @Nine_Function::subStringAtBlank(strip_tags($featuredProduct['intro_text']),50);
        }
        else {
        	$featuredProduct['summary'] = @Nine_Function::subStringAtBlank(strip_tags($featuredProduct['full_text']),50);
        }
		if (null != @$featuredProduct['images']) {
        	$tmp = explode("||", trim($featuredProduct['images'],"|| "));
        	$featuredProduct['main_image'] = @Nine_Function::getThumbImage(@$tmp[0],82,82,null,true);
        }
        else {
        	$featuredProduct['main_image'] = @Nine_Function::getThumbImage('media/userfiles/images/noImage.jpg',82,82,null,true);
        }
        $featuredProduct['url'] = Nine_Route::_(Nine_Registry::getBaseUrl()."product/index/detail/id/{$featuredProduct['product_gid']}");
        
        $this->view->featuredProduct = $featuredProduct;
		
	}
}